//
//  ContentInfoVC.m
//  ProjectsOne
//
//  Created by iOS开发 on 2018/7/10.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "ContentInfoVC.h"
#import "ContentHeadView.h"
#import "UIImage+Custom.h"
#import "ContentInfoCell.h"
#import "CommentHeadView.h"
#import "CommentCell.h"

@interface ContentInfoVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ContentHeadView *headView;
@end

@implementation ContentInfoVC

- (ContentHeadView *)headView {
    if (!_headView) {
        _headView = [[ContentHeadView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 475 / 2)];
        _headView.backgroundColor = [UIColor clearColor];
    }
    return _headView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -kCustomNav, WIDTH, HEIGHT + kCustomNav) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_tableView];
        
        [_tableView registerClass:[ContentInfoCell class] forCellReuseIdentifier:@"infoCell"];
        [_tableView registerClass:[CommentCell class] forCellReuseIdentifier:@"CommentCell"];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.tableHeaderView = self.headView;
    [self setNav];
}

- (void)setNav {
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"内容详情";
    
    // 返回
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"top_return_arrow_n"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"top_return_arrow_p"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    backButton.frame = CGRectMake(0, 0, 40, 40);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    // 右滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
    // 收藏
    UIButton *collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [collectButton addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    collectButton.frame = CGRectMake(0, 0, 40, 40);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:collectButton];
    
    if ([_model.isCollect isEqualToString:@"1"]) {
        [collectButton setImage:[UIImage imageNamed:@"service_details_collection_p"] forState:UIControlStateNormal];
    } else {
        [collectButton setImage:[UIImage imageNamed:@"service_details_collection_n"] forState:UIControlStateNormal];
    }
    
    // 背景
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"service_details_banner"]];
    [self.tableView addSubview:bgView];
    [self.tableView sendSubviewToBack:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.tableView);
    }];
}



#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        if (_model.isHaveMoreComment) {
            return 3;
        } else {
            return _model.commentArr.count;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ContentInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [cell updateUIFromData:_model];
        return cell;
    } else {
        CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (_model.isHaveMoreComment) {
            if (indexPath.row == 2) {
                [cell CellForEndCell:YES];
            } else {
                [cell CellForEndCell:NO];
            }
        } else {
            if (indexPath.row == _model.commentArr.count - 1) {
                [cell CellForEndCell:YES];
            } else {
                [cell CellForEndCell:NO];
            }
        }
        
        [cell updateUIFromData:_model.commentArr[indexPath.row]];
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0?_model.cellHeight:[_model.commentArr[indexPath.row] commentCellHeight];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 1?45:0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CommentHeadView *headView = [[CommentHeadView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 45)];
    headView.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
    
    if (_model.isHaveMoreComment) {
        headView.moreButton.hidden = NO;
    } else {
        headView.moreButton.hidden = YES;
    }
    return headView;
}


#pragma mark - action
// 返回
- (void)popAction {
    [self.navigationController popViewControllerAnimated:YES];
}
// 收藏
- (void)collect:(UIButton *)button {
    
    if ([_model.isCollect isEqualToString:@"1"]) {
        [button setImage:[UIImage imageNamed:@"service_details_collection_n"] forState:UIControlStateNormal];
        [_model setValue:@"0" forKey:@"isCollect"];
    } else {
        [button setImage:[UIImage imageNamed:@"service_details_collection_p"] forState:UIControlStateNormal];
        [_model setValue:@"1" forKey:@"isCollect"];
    }
}



#pragma mark - override
// 滚动改变透明度
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGSize size = [UIImage imageNamed:@"service_details_banner"].size;
    
    CGFloat alpha = scrollView.contentOffset.y / size.height;
    if (alpha <= 1 && alpha >= 0) {
        UIImage *image = [UIImage imageNamed:@"top_navigation_bg"];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageByApplyingAlpha:scrollView.contentOffset.y / size.height image:image] forBarMetrics:UIBarMetricsDefault];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    UIImage *image = [UIImage imageNamed:@"top_navigation_bg"];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageByApplyingAlpha:0 image:image] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    // 自定义返回 右滑开启
    if (self.navigationController.viewControllers.count > 1) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }else{
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
